/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 编辑器;

import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import org.w3c.dom.Node;

/**
 *
 * @author yanxi
 */
public class 对话框 extends javax.swing.JDialog {
    主界面 parent;
            
    /**
     * Creates new form JDialog新增节点
     */
    public 对话框(主界面 parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.parent=parent;
        this.setLocationRelativeTo(parent);
        
        addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                对话框.this.dispose();
            }
        });
        setVisible(true);
        
        编辑Panel p=(编辑Panel)parent.tabpane.getSelectedComponent();  
        TreePath path=p.jTree元素.getSelectionPath();
        if(path==null)this.jButton新增为兄弟节点.setEnabled(false);
        else{
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();
            if(node.getParent()==null)this.jButton新增为兄弟节点.setEnabled(false);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField节点名称 = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jButton新增为子节点 = new javax.swing.JButton();
        jButton新增为兄弟节点 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jLabel1.setText("节点名称：");
        jPanel1.add(jLabel1, java.awt.BorderLayout.LINE_START);

        jTextField节点名称.setText("新节点");
        jPanel1.add(jTextField节点名称, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jButton新增为子节点.setText("新增为子节点");
        jButton新增为子节点.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton新增为子节点ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton新增为子节点);

        jButton新增为兄弟节点.setText("新增为兄弟节点");
        jButton新增为兄弟节点.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton新增为兄弟节点ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton新增为兄弟节点);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton新增为子节点ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton新增为子节点ActionPerformed
        编辑Panel p=(编辑Panel)parent.tabpane.getSelectedComponent();
        if(this.jTextField节点名称.getText()==null||this.jTextField节点名称.getText().isEmpty())
            JOptionPane.showMessageDialog(this, "节点名称不能为空！");
        Node n=p.dom.createElement(this.jTextField节点名称.getText());
        
        TreePath path=p.jTree元素.getSelectionPath();
        if(path!=null){
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent(); 
            DefaultMutableTreeNode nn=new 树节点(n).createTreeNode();
            node.add(nn);
            树节点 xnode=(树节点)node.getUserObject();
            xnode.n.appendChild(n);
            
            p.jTree元素.updateUI();
            p.jTree元素.setSelectionPath(new TreePath(nn.getPath()));
        }
        else{
            if(null==p.jTree元素.getModel().getRoot()){
                p.dom.appendChild(n);
                DefaultMutableTreeNode root=p.createRootNode();
                p.jTree元素.setModel(new DefaultTreeModel(root)); 
                p.jTree元素.setSelectionRow(0);
            }
        }
        
        path=p.jTree元素.getSelectionPath();
        DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();
        if(node.getParent()!=null)this.jButton新增为兄弟节点.setEnabled(true);
        else this.jButton新增为兄弟节点.setEnabled(false);
    }//GEN-LAST:event_jButton新增为子节点ActionPerformed

    private void jButton新增为兄弟节点ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton新增为兄弟节点ActionPerformed
        编辑Panel p=(编辑Panel)parent.tabpane.getSelectedComponent();
        if(this.jTextField节点名称.getText()==null||this.jTextField节点名称.getText().isEmpty())
            JOptionPane.showMessageDialog(this, "节点名称不能为空！");
        Node n=p.dom.createElement(this.jTextField节点名称.getText());
        
        TreePath path=p.jTree元素.getSelectionPath();
        if(path!=null){
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();
            DefaultMutableTreeNode pnode=(DefaultMutableTreeNode)node.getParent(); 
            node=new 树节点(n).createTreeNode();
            pnode.add(node);
            树节点 xnode=(树节点)pnode.getUserObject();
            xnode.n.appendChild(n);
            
            p.jTree元素.updateUI();
            p.jTree元素.setSelectionPath(new TreePath(node.getPath()));
        }
        else{
            if(null==p.jTree元素.getModel().getRoot()){
                p.dom.appendChild(n);
                DefaultMutableTreeNode root=p.createRootNode();
                p.jTree元素.setModel(new DefaultTreeModel(root)); 
                p.jTree元素.setSelectionRow(0);
            }
        }
    }//GEN-LAST:event_jButton新增为兄弟节点ActionPerformed
 

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton新增为兄弟节点;
    private javax.swing.JButton jButton新增为子节点;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField节点名称;
    // End of variables declaration//GEN-END:variables
}
